package com.kxc.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.kxc.dao.HisDataDao;
import com.kxc.dao.OriginalDataDao;
import com.kxc.entity.HisData;
import com.kxc.entity.MoniPoint;
import com.kxc.entity.OriginalData;
import com.kxc.entity.RealData;
import com.kxc.entity.SurfaceDisplacementWarn;
import com.kxc.entity.WarnState;

/**
 * 用指定频率读取司南数据库中的库水位实时数据
 * @author jjq
 * @data  2024年1月31日
 * @version
 */
@Service
public class PublishSfService {

	@Autowired
	private SfDataService dataService;
	@Autowired
	private RealDataService realDataService;
	@Autowired
	private MoniPointService pointService;
	@Autowired
	private HisDataDao hisDataDao;
	@Autowired
	private OriginalDataDao originalDataDao;
	@Autowired
	private SurfaceDisplacementWarnService surfaceDisplacementWarnService;

	public void saveRealTimeDataInfo(List<MoniPoint> points, int sfFrequency) {
		for (MoniPoint moniPoint : points) {
			try {
				Map<String, Double> location = dataService.getGnssrRealTimeDataInfo(moniPoint, sfFrequency);
				Double x1 = location.get("x");
				Double y1 = location.get("y");
				Double h1 = location.get("h");
				if(x1==null||y1==null||h1==null){
					continue;
				}
				WarnState normalState=WarnState.NORMAL;
				RealData realData = realDataService.findByPointNum(moniPoint.getPointNum());
				if(realData==null){
					String data = "{\"data\":{\"xvalue\": '"+String.format("%.3f",x1)+"',\"yvalue\": '"+String.format("%.3f",y1)+"',\"zvalue\": '"+String.format("%.3f",h1)+"',\"horiDis\": '"+String.format("%.3f",0.00)+"',\"horiDisState\":'"+normalState+"',\"horiDisRate\": '"+String.format("%.3f",0.00)+"',\"horiDisRateState\":'"+normalState+"',\"settDis\": '"+String.format("%.3f",0.00)+"',\"settDisState\":'"+normalState+"',\"settDisRate\": '"+String.format("%.3f",0.00)+"',\"settDisRateState\":'"+normalState+"'}}";
					RealData realData1=new RealData();
					realData1.setCollectTime(new Date());
					realData1.setData(data);
					realData1.setMoniItem(moniPoint.getMoniItem());
					realData1.setPointNum(moniPoint.getPointNum());
					realData1.setUploadTime(new Date());
					realDataService.add(realData1);
					continue;
				}

				Map<String, Map<String, String>> dataJsonObj5=(Map<String, Map<String, String>>) JSON.parse(realData.getData());
				Map<String, String> data=dataJsonObj5.get("data");
				double x0 = Double.parseDouble(data.get("xvalue"));
				double y0 = Double.parseDouble(data.get("yvalue"));
				double h0 = Double.parseDouble(data.get("zvalue"));
				double xChange = x1-x0;
				double yChange = y1-y0;
				double hChange = Math.abs(h1-h0);
				double horizontal_real = Math.sqrt(xChange*xChange+yChange*yChange);
				double settlement_real = hChange;
				double settlementRate = settlement_real/(sfFrequency/60);
				double horizontalRate = horizontal_real/(sfFrequency/60);
				MoniPoint point = pointService.getByPointNum(moniPoint.getPointNum());
				double x_original = Double.parseDouble(point.getSufX());
				double y_original = Double.parseDouble(point.getSufY());
				double h_original= Double.parseDouble(point.getSufH());
				
				double xChange_original = x1-x_original;
				double yChange_original = y1-y_original;
				double hChange_original = Math.abs(h1-h_original);
				double horizontal_total = Math.sqrt(xChange_original*xChange_original+yChange_original*yChange_original);
				double settlement_total = hChange_original;

				
				SurfaceDisplacementWarn warnLevel = surfaceDisplacementWarnService.getByPointNum(moniPoint.getPointNum());
				WarnState sState=WarnState.NORMAL;
				WarnState srState=WarnState.NORMAL;
				WarnState lState=WarnState.NORMAL;
				WarnState lrState=WarnState.NORMAL;

				if(warnLevel!=null){
					double sbw = warnLevel.getsBW()!=null&&!"".equals(warnLevel.getsBW())?Double.parseDouble(warnLevel.getsBW()):0;
					double syw = warnLevel.getsYW()!=null&&!"".equals(warnLevel.getsYW())?Double.parseDouble(warnLevel.getsYW()):0;
					double sow = warnLevel.getsOW()!=null&&!"".equals(warnLevel.getsOW())?Double.parseDouble(warnLevel.getsOW()):0;
					double srw = warnLevel.getsRW()!=null&&!"".equals(warnLevel.getsRW())?Double.parseDouble(warnLevel.getsRW()):0;

					double srbw = warnLevel.getSrBW()!=null&&!"".equals(warnLevel.getSrBW())?Double.parseDouble(warnLevel.getSrBW()):0;
					double sryw = warnLevel.getSrYW()!=null&&!"".equals(warnLevel.getSrYW())?Double.parseDouble(warnLevel.getSrYW()):0;
					double srow = warnLevel.getSrOW()!=null&&!"".equals(warnLevel.getSrOW())?Double.parseDouble(warnLevel.getSrOW()):0;
					double srrw = warnLevel.getSrRW()!=null&&!"".equals(warnLevel.getSrRW())?Double.parseDouble(warnLevel.getSrRW()):0;

					double lbw = warnLevel.getlBW()!=null&&!"".equals(warnLevel.getlBW())?Double.parseDouble(warnLevel.getlBW()):0;
					double lyw = warnLevel.getlYW()!=null&&!"".equals(warnLevel.getlYW())?Double.parseDouble(warnLevel.getlYW()):0;
					double low = warnLevel.getlOW()!=null&&!"".equals(warnLevel.getlOW())?Double.parseDouble(warnLevel.getlOW()):0;
					double lrw = warnLevel.getlRW()!=null&&!"".equals(warnLevel.getlRW())?Double.parseDouble(warnLevel.getlRW()):0;

					double lrbw = warnLevel.getLrBW()!=null&&!"".equals(warnLevel.getLrBW())?Double.parseDouble(warnLevel.getLrBW()):0;
					double lryw = warnLevel.getLrYW()!=null&&!"".equals(warnLevel.getLrYW())?Double.parseDouble(warnLevel.getLrYW()):0;
					double lrow = warnLevel.getLrOW()!=null&&!"".equals(warnLevel.getLrOW())?Double.parseDouble(warnLevel.getLrOW()):0;
					double lrrw = warnLevel.getLrRW()!=null&&!"".equals(warnLevel.getLrRW())?Double.parseDouble(warnLevel.getLrRW()):0;

					sState = pointService.warnStateJudge(settlement_real,sbw,sow,syw,srw);//沉降
					srState = pointService.warnStateJudge(settlementRate,srbw,srow,sryw,srrw);
					lState = pointService.warnStateJudge(horizontal_real,lbw,low,lyw,lrw);//水平
					lrState = pointService.warnStateJudge(horizontalRate,lrbw,lrow,lryw,lrrw);
				}
				//{"data":{"xvalue": '4077971.3004',"yvalue": '861.2783',"zvalue":"861.2783",
				//"horiDis":"0.2","horiDisState":"RED","horiDisRate":"1","horiDisRateState":"RED",
				//"settDis":"0.2","settDisState":"RED","settDisRate":"1","settDisRateState":"RED"}}
				//,\"horiDis\": '"+String.format("%.3f",horizontal)+"',\"horiDisState\":'"+lState+"'
				String data1 = "{\"data\":{\"xvalue\": '"+String.format("%.3f",x1)+"',\"yvalue\": '"+String.format("%.3f",y1)+"',\"zvalue\": '"+String.format("%.3f",h1)
				+"',\"xChange\": '"+String.format("%.3f",xChange)+"',\"yChange\": '"+String.format("%.3f",yChange)+"',\"zChange\": '"+String.format("%.3f",hChange)
				+"',\"horizontal_total\": '"+String.format("%.3f",horizontal_total)+"',\"settlement_total\": '"+String.format("%.3f",settlement_total)+"',\"horiDis\": '"+String.format("%.3f",horizontal_real)+"',\"horiDisState\":'"+lState+"',\"horiDisRate\": '"
						+String.format("%.3f",horizontalRate)+"',\"horiDisRateState\":'"+lrState+"',\"settDis\": '"+String.format("%.3f",settlement_real)
						+"',\"settDisState\":'"+sState+"',\"settDisRate\": '"+String.format("%.3f",settlementRate)+"',\"settDisRateState\":'"+srState+"'}}";
				System.out.println("data:"+data1);
				realData.setCollectTime(new Date());
				realData.setData(data1);
				realData.setMoniItem(point.getMoniItem());
				realData.setPointNum(point.getPointNum());
				realData.setUploadTime(new Date());
				realDataService.update(realData);
				hisDataDao.save(new HisData(moniPoint.getPointNum(), point.getMoniItem(), data1,new Date(), new Date()));
				originalDataDao.save(new OriginalData(point.getDev(), "SurfaceDisplacement","x:"+x1+" y:"+y1+" z:"+h1, new Date(), new Date(),""));

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

}
